package com.openapi.protocol.encode;

import com.openapi.data.JT808PushData;
import exsun.jt808.data.base.JT808Category;
import exsun.jt808.data.distribute_data.DistributeEntity;
import exsun.jt808.data.distribute_data.Qingdao.*;
import exsun.jt808.data.distribute_data.jt808.AreaProperty;
import exsun.jt808.data.util.parse.BCD8421Operater;
import exsun.jt808.data.util.parse.BitOperator;


public class PackageQingdao {
    //处置证下发
    public static JT808PushData HandleIssue(HandleIssue data){
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo2Bytes(data.getMsgId()),
                BitOperator.integerTo4Bytes(data.getId()),
                BitOperator.integerTo1Bytes(data.getOperation()),
                BitOperator.integerTo1Bytes(data.getBusinessValue()),
                BCD8421Operater.DateToBCD6(data.getStartTime()),
                BCD8421Operater.DateToBCD6(data.getEndTime()),
                BitOperator.stringToBytes(data.getContent())
        );
        return new JT808PushData(0x8620,msgBodyBytes,data);
    }
    //管控开关定义
    public static JT808PushData ControlSwitchDefinition(ControlSwitchDefinition data){
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo2Bytes(data.getMsgId()),
                BitOperator.integerTo4Bytes(data.getId()),
                BitOperator.integerTo1Bytes(data.getLiftSwitch()),
                BitOperator.integerTo1Bytes(data.getSpeeding()),
                BitOperator.integerTo1Bytes(data.getBeyondBoundsAlarm()),
                BitOperator.integerTo1Bytes(data.getUploadAlarm()),
                BitOperator.integerTo1Bytes(data.getUnlicensedAlarm()),
                BitOperator.integerTo1Bytes(data.getDriverAlarm()),
                BitOperator.integerTo1Bytes(data.getBreakAlarm()),
                BitOperator.integerTo1Bytes(data.getKeep()));
        return new JT808PushData(0xF114,msgBodyBytes,data);

    }

    //管控开关查询
    public static JT808PushData controlSwitchSort(DistributeEntity data){
        //消息体为空
        byte[] body = new byte[0];
        return new JT808PushData(0xF115,body,data);

    }


    //管控开关状态控制
    public static JT808PushData controlSwitchStatus(ControlSwitchStatus data){
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo1Bytes(data.getLiftSwitch()),
                BitOperator.integerTo1Bytes(data.getSpeedSwitch()),
                BitOperator.integerTo1Bytes(data.getLockCarSwitch()));
        return new JT808PushData(0xF116, msgBodyBytes, data);
    }

    //管控开关状态查询
    public static JT808PushData controlSwitchStatusSort(DistributeEntity data){

        //消息体为空
        byte[] body = new byte[0];
        return new JT808PushData(0xF117,body,data);
    }

    // 新增多边型区域（围栏）
    public static JT808PushData addPolygonArea(AddPolygonArea data){
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getFlag()),
                 BitOperator.integerTo1Bytes(data.getAreaCount()));
        for (PolygonAreaInfo item : data.getItems()){
            int areaFlag = getAreaProp_PolygonArea(item.getAreaProperty());
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,BitOperator.integerTo4Bytes(item.getSignId()),
                    BitOperator.integerTo2Bytes(areaFlag),
                    BCD8421Operater.DateToBCD6(item.getStartTime()),
                    BCD8421Operater.DateToBCD6(item.getEndTime()),
                    BitOperator.integerTo1Bytes(item.getBusinessValue()),
                    BitOperator.integerTo2Bytes(item.getPoint()));
                    for(int i = 0; i<item.getPoint(); i++){
                        msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                                BitOperator.integerTo4Bytes((int)(item.getLat() * 1000000)),
                                BitOperator.integerTo4Bytes((int)(item.getLng() * 1000000)));
                    }

        }
        return new JT808PushData(0x860D,msgBodyBytes,data);

    }

    private static int getAreaProp_PolygonArea(AreaProperty item){
        String[] hexFlag = new String[16];
        hexFlag[0] = item.isbTime() ? "1" : "0";
        hexFlag[1] = item.isbSpeed() ? "1" : "0";
        hexFlag[2] = item.isbInToDriver() ? "1" : "0";
        hexFlag[3] = item.isbInToPlat() ? "1" : "0";
        hexFlag[4] = item.isbOutToDriver() ? "1" : "0";
        hexFlag[5] = item.isbOutToPlat() ? "1" : "0";
        hexFlag[6] = String.valueOf(item.getLatType());
        hexFlag[7] = String.valueOf(item.getLongType());
        hexFlag[8] = String.valueOf(item.getAllowOpenDoor());
        hexFlag[9] = String.valueOf(item.getAreaType());
        hexFlag[10] = String.valueOf(item.getAreaType());
        hexFlag[11] = String.valueOf(item.getAreaType());
        if (item.getAreaType() == 1){
            String.valueOf(hexFlag[9] == "0" && hexFlag[10] == "0" && hexFlag[11] == "1");
        }else if (item.getAreaType() == 2){
            String.valueOf(hexFlag[9] == "0" && hexFlag[10] == "1" && hexFlag[11] == "0");
        }else if (item.getAreaType() == 3){
            String.valueOf(hexFlag[9] == "0" && hexFlag[10] == "1" && hexFlag[11] == "1");
        }else if (item.getAreaType() == 4){
            String.valueOf(hexFlag[9] == "1" && hexFlag[10] == "0" && hexFlag[11] == "0");
        }else if (item.getAreaType() == 5){
            String.valueOf(hexFlag[9] == "1" && hexFlag[10] == "0" && hexFlag[11] == "1");
        }
        hexFlag[12] = "0";
        hexFlag[13] = "0";
        hexFlag[14] = String.valueOf(item.getOpenCommunicationModule());
        hexFlag[15] = String.valueOf(item.getCollectGNSSInfo());
        String str = "";
        for (int n = hexFlag.length - 1; n >= 0; n--)
        {
            str += hexFlag[n];
        }

        int areaFlag = Integer.parseInt(str, 2);
        return areaFlag;
    }

    //删除多边形区域
    public static  JT808PushData deletePolygonArea(DeletePolygonArea data){
        byte[] msgBodyBytes = BitOperator.integerTo1Bytes(data.getSignIds().size());
        for (Integer signId : data.getSignIds()){
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,BitOperator.integerTo4Bytes(signId));
        }
        return new JT808PushData(0x860E,msgBodyBytes,data);
    }

    //查询多边形区域
    public static JT808PushData sortPolygonArea(DistributeEntity data){
        //消息体为空
        byte[] body = new byte[0];
        return new JT808PushData(0x860F,body,data);

    }

    //新增线路
    public static JT808PushData addRoute(AddRoute data){
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getFlag()),
                BitOperator.integerTo4Bytes(data.getSignId()),
                BitOperator.integerTo1Bytes(data.getRouteWidth()),
                BitOperator.integerTo1Bytes(data.getBusinessValue()),
                BitOperator.integerTo2Bytes(data.getPoint()));
                for(int i = 0; i<data.getPoint(); i++){
                    msgBodyBytes = BitOperator.concatAll(msgBodyBytes,
                    BitOperator.integerTo4Bytes((int)(data.getLat() * 1000000)),
                    BitOperator.integerTo4Bytes((int)(data.getLng() * 1000000)));
        }
        return new JT808PushData(0x8610,msgBodyBytes,data);
    }

    //删除线路
    public static JT808PushData deleteRoute(DeleteRoute data){
        byte[] msgBodyBytes = BitOperator.integerTo1Bytes(data.getSignIds().size());
        for (Integer routeId : data.getSignIds()){
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes, BitOperator.integerTo4Bytes(routeId));
        }

        return new JT808PushData(0x8611,msgBodyBytes,data);
    }

    //查询线路
    public static JT808PushData sortRoute(DistributeEntity data){
        //消息体为空
        byte[] body = new byte[0];
        return new JT808PushData(0x8612,body,data);
    }

    //限举指令下发
    public static JT808PushData limitLeftIssue(LimitLeftIssue data){
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getLiftType()));
        return new JT808PushData(0x8622,msgBodyBytes,data);

    }

    //限速指令下发
    public static JT808PushData limitSpeedIssue(LimitSpeedIssue data){
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getSpeedType()),
                BitOperator.integerTo1Bytes(data.getSpeedValue()));
        return new JT808PushData(0x8623,msgBodyBytes,data);
    }

    //锁车指令
    public static JT808PushData lockCar(LockCar data){
        byte[] msgBodyBytes = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getCommand()));
        return new JT808PushData(0xF110,msgBodyBytes,data);
    }

    //驾驶员人脸库同步
    public static JT808PushData driverFace(DriverFace data){
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo1Bytes(data.getType()),
                BitOperator.integerTo2Bytes(data.getFaceInfoList().size()));
        for (FaceInfo items : data.getFaceInfoList()){
            msgBodyBytes = BitOperator.concatAll(msgBodyBytes,BitOperator.integerTo1Bytes(items.getDriverId().length()),
                    BitOperator.stringToBytes(items.getDriverId()),
                    BitOperator.integerTo1Bytes(items.getJobSeniorityCard().length()),
                    BitOperator.stringToBytes(items.getJobSeniorityCard()),
                    BitOperator.integerTo1Bytes(items.getDriverName().length()),
                    BitOperator.stringToBytes(items.getDriverName()),
                    BitOperator.integerTo1Bytes(items.getFacePictureAgreement()),
                    BitOperator.integerTo1Bytes(items.getFacePictureId().length()),
                    BitOperator.stringToBytes(items.getFacePictureId()),
                    BitOperator.integerTo1Bytes(items.getFacePictureUrl().length()),
                    BitOperator.stringToBytes(items.getFacePictureUrl()));
        }
        return new JT808PushData(0x8E21,msgBodyBytes,data);
    }

    //驾驶员信息查询
    public static JT808PushData driverMsgSort(DistributeEntity data){
        //消息体为空
        byte[] body = new byte[0];
        return new JT808PushData(0x8E12,body,data);

    }

    //驾驶员对比结果上报应答
    public static JT808PushData driverContrastResultResponse(DriverContrastResultResponse data){
        byte[] body = null;
        if (data.getMissItems() == null || data.getMissItems().size() == 0){
            body = BitOperator.integerTo2Bytes(data.getOrgSerialNum());
        }else {
            body = BitOperator.concatAll(BitOperator.integerTo2Bytes(data.getOrgSerialNum()),
                    BitOperator.integerTo2Bytes(data.getMissItems().size()));
            for (Integer item: data.getMissItems()) {
                body = BitOperator.concatAll(
                        body,
                        BitOperator.integerTo2Bytes(item));
            }
        }
        return new JT808PushData( 0x8E10,body,data,JT808Category.platformReplay);
    }

    //设备录入驾驶员信息上报
    public static JT808PushData deviceInput(DeviceInput data){
        byte[] body = null;
        if (data.getMissItems() == null || data.getMissItems().size() == 0){
            body = BitOperator.integerTo2Bytes(data.getOrgSerialNum());
        }else {
            body = BitOperator.concatAll(BitOperator.integerTo2Bytes(data.getOrgSerialNum()),
                    BitOperator.integerTo2Bytes(data.getMissItems().size()));
            for (Integer item: data.getMissItems()) {
                body = BitOperator.concatAll(
                        body,
                        BitOperator.integerTo2Bytes(item));
            }
        }
        return new JT808PushData( 0x8E13,body,data,JT808Category.platformReplay);
    }

    //远程升级指令
    public static JT808PushData remoteUpgrade(RemoteUpgrade data) {

        byte[] body = BitOperator.concatAll(
                BitOperator.integerTo1Bytes(data.getUpgradeType()),
                BitOperator.integerTo1Bytes(data.getVNumber().length()),
                BitOperator.stringToBytes(data.getVNumber()),
                BCD8421Operater.DateToBCD6(data.getSynchronizationTime()),
                BitOperator.integerTo1Bytes(data.getFtpipAddress().length()),
                BitOperator.stringToBytes(data.getFtpipAddress()),
                BitOperator.integerTo2Bytes(data.getPort()),
                BitOperator.integerTo1Bytes(data.getFtpVersionFileAddress().length()),
                BitOperator.stringToBytes(data.getFtpVersionFileAddress()),
                BitOperator.integerTo1Bytes(data.getFileName().length()),
                BitOperator.stringToBytes(data.getFileName()),
                BitOperator.integerTo1Bytes(data.getFtpAccount().length()),
                BitOperator.stringToBytes(data.getFtpAccount()),
                BitOperator.integerTo1Bytes(data.getFtpPassword().length()),
                BitOperator.stringToBytes(data.getFtpPassword()));
        return new JT808PushData(0x8901,body,data);
    }
    //设备版本查询
    public static JT808PushData deviceVersionQuery(DeviceVersionQuery data){
        byte[] body = BitOperator.integerTo1Bytes(data.getDeviceType());
        return new JT808PushData(0x8902,body,data);
    }


}
